package com.infodms.dms.actions.sales.marketmanage.planmanage;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.materialManager.MaterialGroupManagerDao;
import com.infodms.dms.dao.sales.marketmanage.planmanage.ActivitiesMediaCostDao;
import com.infodms.dms.dao.sales.ordermanage.delivery.OrderDeliveryDao;
import com.infodms.dms.dao.sales.planmanage.SpacePlanManageDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.util.CommonUtils;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PageResult;

public class ActivitiesMediaCost {
	public Logger logger = Logger.getLogger(ActivitiesSpacePlanReport.class);   
	SpacePlanManageDao dao  = SpacePlanManageDao.getInstance();
	private ActionContext act = ActionContext.getContext();
	RequestWrapper request = act.getRequest();
	
	/*--------------------------------------------------   url常量  --------------------------------------------------*/
	
	private final String MEDIA_COST_INIT_URL = "/jsp/sales/marketmanage/planmanage/mediaCostSearch.jsp" ;  // 媒体信息查询
	
	/*--------------------------------------------------   对应action方法   --------------------------------------------------*/
	
	public void activitiesPlanReportInit(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(logonUser.getPoseId().toString());

			Long orgId = logonUser.getOrgId() ;
			String dutyType = logonUser.getDutyType() ;
			
			List<Map<String, Object>> orgList = OrderDeliveryDao.getOemOrg(dutyType, orgId) ;
			
			act.setOutData("orgList", orgList) ;
			act.setOutData("areaList", areaList);
			
			act.setForword(MEDIA_COST_INIT_URL);
		}catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"媒体信息查询页面初始化");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	public void activitiesSpacePlanQuery() {
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			String orgId = logonUser.getOrgId().toString() ;
			String poseId = logonUser.getPoseId().toString() ;
			String dutyType = logonUser.getDutyType() ;
			
			String pageOrgId = CommonUtils.checkNull(request.getParamValue("pageOrgId")) ;
			String areaId = CommonUtils.checkNull(request.getParamValue("areaId")) ;
			String startDate = CommonUtils.checkNull(request.getParamValue("startDate")).trim() ;
			String endDate = CommonUtils.checkNull(request.getParamValue("endDate")).trim() ;
			String region = CommonUtils.checkNull(request.getParamValue("region")).trim() ;
			String model = CommonUtils.checkNull(request.getParamValue("model")).trim() ;
			String mediaType = CommonUtils.checkNull(request.getParamValue("mediaType")).trim() ;
			String costType = CommonUtils.checkNull(request.getParamValue("costType")).trim() ;
			String mediaName = CommonUtils.checkNull(request.getParamValue("mediaName")).trim() ;
			
			Integer curPage = request.getParamValue("curPage") !=null ? Integer.parseInt(request.getParamValue("curPage")) : 1;
			
			Map<String, Object> mediaCostMap = ActivitiesMediaCostDao.meidaCostQuery(costType, dutyType, pageOrgId, orgId, startDate, endDate, areaId, region, model, mediaType, poseId, mediaName, Constant.PAGE_SIZE, curPage) ;
			
			List<Map<String, Object>> mediaCostList = null ;
			PageResult<Map<String, Object>> mediaCostResult = new PageResult<Map<String,Object>>() ;
			if(!CommonUtils.isNullMap(mediaCostMap)) {
				mediaCostList = (List<Map<String, Object>>)mediaCostMap.get("mediaCostList") ;
				mediaCostResult = (PageResult<Map<String, Object>>)mediaCostMap.get("mediaCostResult") ;
			}
		
			int count = 0 ;
			double account = 0 ;
			
			if (!CommonUtils.isNullList(mediaCostList)) {
				int len = mediaCostList.size() ;
				
				for(int i=0; i<len; i++) {
					count += Integer.parseInt(mediaCostList.get(i).get("TOTAL_COUNT").toString()) ;
					account += Double.parseDouble(mediaCostList.get(i).get("PLAN_COST").toString()) ;
				}
			}
			
			act.setOutData("count", count) ;
			act.setOutData("account", account) ;
			act.setOutData("ps", mediaCostResult) ;
		} catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"区域活动方案查询");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
}
